package com.model.project;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.TableGenerator;

import com.core.protocols.IKVObject;
import com.core.utils.KeyValueDictionary;
import com.core.utils.LocalizableString;
import com.model.KeyValue;
import com.model.user.User;

@Entity
public class TeamMember
        implements IKVObject {

	private Integer            id;
	private String             name;
	private String             description;
	private ProjectItem        parentProject;
	private TeamMemberCategory teamMemberCategory = new TeamMemberCategory();



	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	public TeamMemberCategory getTeamMemberCategory () {

		return teamMemberCategory;
	}



	public void setTeamMemberCategory ( TeamMemberCategory teamMemberCategory ) {

		this.teamMemberCategory = teamMemberCategory;
	}



	@ManyToOne
	public ProjectItem getParentProject () {

		return parentProject;
	}



	public void setParentProject ( ProjectItem parentProject ) {

		this.parentProject = parentProject;
	}



	@Id
	@TableGenerator(allocationSize = 10, table = "Generators", pkColumnName = "generatorName", pkColumnValue = "generatorUser", name = "generatorUser")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorUser")
	public Integer getId () {

		return id;
	}



	public void setId ( Integer id ) {

		this.id = id;
	}



	public String getName () {

		return name;
	}



	public void setName ( String name ) {

		this.name = name;
	}



	@Lob
	public String getDescription () {

		return description;
	}



	public void setDescription ( String description ) {

		this.description = description;
	}



	/*
	 * KVO
	 */
	public KeyValueDictionary toDictionary () {

		KeyValueDictionary result = new KeyValueDictionary();
		return result;
	}



	@Override
	public void assignTo ( IKVObject object ) {

		TeamMember member = (TeamMember) object;
		member.setDescription(this.getDescription());
		member.setName(this.getName());
		member.getTeamMemberCategory().setUser(this.getTeamMemberCategory().getUser());

	}



	@Override
	public KeyValue idValue () {

		String strIdValue = this.getId().toString();
		return new KeyValue(this.fieldNameAsId(), strIdValue, false);

	}



	@Override
	public String fieldNameAsId () {

		return "id";
	}



	@Override
	public KeyValue nameValue () {

		String strIdValue = null;
		User user = this.getTeamMemberCategory().getUser();
		if ( user != null ) {
			strIdValue = user.getName();
		}
		return new KeyValue(this.fieldNameAsName(), strIdValue, false);

	}



	@Override
	public String fieldNameAsName () {

		return "name";
	}



	public String localizableDescription () {

		return LocalizableString.sharedInstance().localizable("entity.Team.description");
	}
}
